/**
 * 桌子界面
 * by wyy
 */
class deskScene extends GameObject implements eui.UIComponent {

    public roundLbl: eui.Label;
    public deskGrp: eui.Group;
    public diceGrp: eui.Group;
    private desk;
    public quitBtn: eui.Button;

    public displayGrp: eui.Group;
    public nameLbl: eui.Label;
    public timeLbl: eui.Label;
    public tipLbl: eui.Label;
    public ruleBtn: eui.Button;


    type = "desk";

    widthPercent = 1;
    heightPercent = 1;

    private data;
    constructor(d) {
        super();
        this.skinName = "resource/eui_skins/game_skins/deskSceneSkin.exml";
        this.data = d;
    };
    addToStage() { }
    firstCreate() {
        this.nameLbl.text = globalObject.getInstance().player.Username;

        //更新自己的数据显示
        nano.request("gamemanager.getmyselfinfo", {}, (data) => {
            globalObject.getInstance().player = data["data"];
            globalObject.getInstance().debugConsole("更新自己的数据显示", data);
            switch (this.data.data.max_player) {
                case 9:
                    this.desk = new nineDesk(this.data);
                    break;
                case 10:
                    this.desk = new tenDesk(this.data);
                    break;
                case 11:
                    this.desk = new elevenDesk(this.data);
                    break;
                default:
                    this.desk = new nineDesk(this.data);
                    break;
            }
            this.deskGrp.addChild(this.desk);
            this.diceGrp.addChild(new diceGrp(this.data.data.num, this.desk));
        });
        this.roundLbl.text = "第" + this.data.data.num + "轮 第" + this.data.data.desk_id + "桌";
        this.ruleBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,
            () => { SceneManager.getInstance().showWindow(new helpScene()) }, this);

        this.quitBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.quit, this);

        if (globalObject.getInstance().createDeskListener) {
            nano.on("playerin", (data) => {
                if (SceneManager.getInstance().getCurrentScene().type == "desk") {
                    globalObject.getInstance().debugConsole("同桌玩家加入：", data);
                    if ((<deskScene>SceneManager.getInstance().getCurrentScene()).desk.playerList[data.uid]) {
                        (<deskScene>SceneManager.getInstance().getCurrentScene()).desk.playerList[data.uid].offlineGrp.visible = false;
                        return;
                    }
                    (<deskScene>SceneManager.getInstance().getCurrentScene()).desk.addPlayer(data);
                }
            });
            nano.on("TZresult", (data) => {
                if (SceneManager.getInstance().getCurrentScene().type == "desk") {
                    globalObject.getInstance().debugConsole("同桌玩家投掷结果：", data);
                    let players = data.players;
                    let cur_result;
                    for (let i in players) {
                        (<deskScene>SceneManager.getInstance().getCurrentScene()).desk.playerList[players[i].id].renewTime(players[i].roll_time);
                        if (players[i].id == data.cur_id) {
                            cur_result = players[i].result;
                        }
                    }
                    (<deskScene>SceneManager.getInstance().getCurrentScene()).desk.playerList[data.cur_id].playDice(cur_result);
                }
            });
            nano.on("offline", (d) => {
                if (SceneManager.getInstance().getCurrentScene().type == "desk") {
                    globalObject.getInstance().debugConsole("同桌玩家离线：", d);
                    if ((<deskScene>SceneManager.getInstance().getCurrentScene()).desk.playerList[d.off_id]) {
                        (<deskScene>SceneManager.getInstance().getCurrentScene()).desk.playerList[d.off_id].offlineGrp.visible = true;
                    }
                }
            });
            nano.on("gameover", (d) => {
                if (SceneManager.getInstance().getCurrentScene().type == "desk") {
                    globalObject.getInstance().showTip(d.message);
                    nano.request("gamemanager.leavedesk", {}, () => { });
                    SceneManager.getInstance().closeWindow();
                    SceneManager.getInstance().changeScene(new introScene());
                }
            });
            nano.on("gameend", (data) => {
                if (SceneManager.getInstance().getCurrentScene().type == "desk") {
                    let scene = (<deskScene>SceneManager.getInstance().getCurrentScene())
                    scene.setTipText(data.message);
                }
            });
            globalObject.getInstance().createDeskListener = false;
        }
        this.ruleBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,
            () => { SceneManager.getInstance().showWindow(new helpScene()) }, this);

    }
    setTipText(s) {
        if (s.indexOf("/")) {
            let arr = s.split("/");
            this.timeLbl.text = arr[1];
            this.tipLbl.text = arr[0];
        }
        else {
            this.timeLbl.text = s;
        }
    }
    private quit() {
        nano.request("gamemanager.leavedesk", {}, () => { });
        SceneManager.getInstance().closeWindow();
        SceneManager.getInstance().changeScene(new hallScene());
    }

    onRemove() { }

}